// pages/mySelf/Index/index.js
const common = require('../../../../../public.js')
const utils = require('../../utils.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    token: '',
    // past: true,
    type: 2,
    userAssets: 0, //余额
    discountCouponNums: 0, //优惠卷
    benefitCouponNums: 0, // 惠民券
    storeFavolist: 0, //收藏
    totalStoreDrawOrderNums: 0, // 抽奖订单总数
    storeDrawOrder: '', // 订单详情
    storeDrawOrderFlag: '', // 是否开奖
    tabs:'managerId',
    active:4
  },

  /**
   * 生命周期函数--监听页面加载
   */
    // 切换tabbar
    onChangeTabbars(e) { //切换底部导航
      console.log(e)
      const index = e.detail.index
      const url = e.detail.url + '?fl=lanling' 
      console.log(url)
      if (index != this.data.active) {
        utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
      }
    },

  onLoad: function (options) {
  },
  toNianKa(){
    wx.navigateTo({
      url:'/packageA/pages/indexnian/indexni',
    })
  },
    // 跳转会员卡订单
    toNianOrder(){
      wx.navigateTo({
        url: '/pages/ScenicArea/NianCardOrder/index',
      })
    },
    
  onShow() {
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token
        }, function () {
          that.getUserCenterInfo()
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    })
  },
  toLogin() { // 去登陆
    common.showLoginPage()
  },

  applyEnter() { // 点击申请入驻
    common.showToast('请登录兰陵旅游游客端app查看')
  },
  contactServer() { //联系客服
    wx.navigateTo({
      url: '/packageB/pages/custService/index',
    })
  },
  getUserCenterInfo() { // 获取用户信息
    if (this.data.userInfo === null) {
      return
    }
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token
    }
    common.getJson("/user/getUserCenterInfo", tempJson, function (res) {
      if (res.status === 200) {
        console.log('res', res)
        that.setData({
          userAssets: parseFloat(res.data.userAssets).toFixed(2),
          discountCouponNums: res.data.discountCouponNums,
          storeFavolist: res.data.storeFavolist,
          totalStoreDrawOrderNums: res.data.totalStoreDrawOrderNums,
          storeDrawOrder: res.data.storeDrawOrder ? res.data.storeDrawOrder : '',
          storeDrawOrderFlag: res.data.storeDrawOrderFlag ? res.data.storeDrawOrderFlag : '',
          benefitCouponNums: res.data.benefitCouponNums
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function (responseType) {
      if (responseType === 1) {
        // 登录过期
        that.clearState()
      }
    })
  },
  clearState() {
    this.setData({
      userInfo: null,
      token: '',
      userAssets: 0,
      discountCouponNums: 0,
      storeFavolist: [],
      totalStoreDrawOrderNums: 0,
      storeDrawOrder: '',
      storeDrawOrderFlag: '',
      benefitCouponNums: 0
    })
  },
  updateData() {
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
        }, function () {
          // that.getActList();
          // that.getCode()
          that.getUserCenterInfo()
        })
      }
    })
  },
  // getActList() {
  //   var that = this;
  //   wx.request({
  //     url: HttpUrl + '/activity/selectByUser',
  //     data: {
  //       userId: that.data.userInfo.id
  //     },
  //     method: "GET",
  //     header: {
  //       'content-type': 'application/json' // 默认值
  //     },
  //     success: function (res) {
  //       console.log('活动', res)
  //       if (res.data.data.totalData == 0) {
  //         that.setData({
  //           List: []
  //         })
  //       } else {
  //         that.setData({
  //           List: res.data.data.result_list[0]
  //         })
  //       }
  //     }
  //   })
  // },
  // getCode() {
  //   var that = this;
  //   wx.request({
  //     url: HttpUrl + '/isPromoter',
  //     method: 'GET',
  //     data: {
  //       userId: that.data.userInfo.id,
  //       token: that.data.token
  //     },
  //     header: {
  //       'content-type': 'application/json' // 默认值
  //     },
  //     success: function (res) {
  //       if (res.data.status == 200) {
  //         if (!res.data.data == "") {
  //           that.setData({
  //             code: res.data.data,
  //             type: 1,
  //             past: false
  //           }, () => {
  //             that.getCodeData()
  //           })
  //         } else {
  //           that.setData({
  //             past: true,
  //             type: 2
  //           })
  //         }
  //       }
  //     }
  //   })
  // },
  // getCodeData() {
  //   var that = this;
  //   wx.request({
  //     url: HttpUrl + '/selectUserMoney',
  //     method: 'GET',
  //     data: {
  //       userId: that.data.userInfo.id,
  //       token: that.data.token,
  //       inviteCode: that.data.code
  //     },
  //     header: {
  //       'content-type': 'application/json' // 默认值
  //     },
  //     success: function (res) {
  //       if (res.data.status == 200) {
  //         that.setData({
  //           codeData: res.data.data //守艺人数据
  //         })
  //       }
  //     }
  //   })
  // },
  toCollection() { // 跳转收藏
    this.checkLoginToJump('/pages/collection/index')
  },
  toWallet() { // 跳转钱包
    this.checkLoginToJump('/pages/mySelf/wallet/index')
  },
  toCoupons() { // 跳转优惠券中心
    this.checkLoginToJump('/pages/coupons/myCoupons/index')
  },
  toCultureCoupons() {
    this.checkLoginToJump('/pages/cultureCoupons/myCultureCoupons/index')
  },
  toDraw() {
    common.showToast('请移步兰陵旅游游客端app')
  },
  toAddressManager() {
    // this.checkLoginToJump('/pages/Goods/Address/list/index?from=mySelf')
    common.showToast('已经是最新版本')
  },
  /**
   * 跳转到实名认证页面
   */
  toRealName() {
    if (this.data.userInfo && this.data.userInfo.isRealname) {
      common.showToast('您已实名')
    } else {
      this.checkLoginToJump('/pages/realIdent/index')
    }
  },
  /**
   * 跳转页面，该接口会验证用户是否登录
   * @param {string} url 跳转连接
   */
  checkLoginToJump(url) {
    if (!this.data.userInfo) {
      common.showToast('您尚未登录，请登录后再查看吧~')
    } else {
      wx.navigateTo({
        url
      })
    }
  },
  toVip() {
    this.checkLoginToJump('/pages/vip/index')
  },
  logout() {
    wx.removeStorageSync('user')
    common.showToast('您已退出登录')
    this.clearState()
  }
})